﻿using db_manage.dll.manage;
using db_manage.model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace db_manage.page
{
    public partial class form_db_config : Form
    {
        public form_db_config()
        {
            InitializeComponent();
            update_data();
            ((DataGridViewComboBoxColumn)this.dgv_db_config.Columns["type"]).DataSource = Enum.GetValues(typeof(db_type));
        }

        private void update_data()
        {
            this.dgv_db_config.DataSource = new List<db_link_info>(manage.db_manage_config.db_link_info);
        }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_save_Click(object sender, EventArgs e)
        {
            manage.db_manage_config = manage.db_manage_config;
            MessageBox.Show("操作完成");
        }

        /// <summary>
        /// 新增行事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgv_db_config_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            manage.db_manage_config.db_link_info
                .Where(x => string.IsNullOrEmpty(x.tip))
                ?.ToList()
                ?.ForEach(x =>
                {
                    x.tip = Guid.NewGuid().ToString("N");
                });
        }

        /// <summary>
        /// 新增行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_add_Click(object sender, EventArgs e)
        {
            manage.db_manage_config.db_link_info?.Add(new db_link_info());
            update_data();
        }

        /// <summary>
        /// 删除行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bt_del_Click(object sender, EventArgs e)
        {
            if (this.dgv_db_config.CurrentRow == null)
            {
                return;
            }
            var index = this.dgv_db_config.CurrentRow.Index;
            manage.db_manage_config.db_link_info.RemoveAt(index);
            update_data();
        }
    }
}
